const GroupBy = require("../template")

function testGroupByPerformance() {
  console.log(
    "1. 数组分组性能测试 - 大量数据分组:",
    (() => {
      const largeArray = Array.from({ length: 10000 }, (_, i) => ({ id: i % 1000, value: i }))
      const start = performance.now()
      const result = GroupBy.groupBy(largeArray, 'id')
      const end = performance.now()
      return (end - start) < 100 && Object.keys(result).length === 1000
    })()
  )

  console.log(
    "2. 数组分组性能测试 - 内存使用:",
    (() => {
      const initialMemory = process.memoryUsage().heapUsed
      const largeArray = Array.from({ length: 10000 }, (_, i) => ({ id: i % 1000, value: i }))
      const result = GroupBy.groupBy(largeArray, 'id')
      const finalMemory = process.memoryUsage().heapUsed
      const memoryIncrease = finalMemory - initialMemory
      return memoryIncrease < 1024 * 1024 // 内存增长应该小于1MB
    })()
  )

  console.log(
    "3. 数组分组性能测试 - 函数分组:",
    (() => {
      const largeArray = Array.from({ length: 10000 }, (_, i) => i)
      const start = performance.now()
      const result = GroupBy.groupBy(largeArray, x => x % 100)
      const end = performance.now()
      return (end - start) < 50 && Object.keys(result).length === 100
    })()
  )

  console.log(
    "4. 数组分组性能测试 - 深度分组:",
    (() => {
      const largeArray = Array.from({ length: 1000 }, (_, i) => ({
        user: { profile: { level: i % 10 } }
      }))
      const start = performance.now()
      const result = GroupBy.groupByDeep(largeArray, 'user.profile.level', 3)
      const end = performance.now()
      return (end - start) < 50 && Object.keys(result).length === 10
    })()
  )

  console.log(
    "5. 数组分组性能测试 - 多条件分组:",
    (() => {
      const largeArray = Array.from({ length: 1000 }, (_, i) => ({
        id: i % 100,
        category: i % 10,
        value: i
      }))
      const start = performance.now()
      const result = GroupBy.groupByMultiple(largeArray, 'id', 'category')
      const end = performance.now()
      return (end - start) < 50 && Object.keys(result).length === 100
    })()
  )

  console.log(
    "6. 数组分组性能测试 - 条件过滤分组:",
    (() => {
      const largeArray = Array.from({ length: 10000 }, (_, i) => i)
      const start = performance.now()
      const result = GroupBy.groupByWithFilter(largeArray, x => x % 2 === 0 ? 'even' : 'odd', x => x > 5000)
      const end = performance.now()
      return (end - start) < 50 && Object.keys(result).length === 2
    })()
  )

  console.log(
    "7. 数组分组性能测试 - 映射分组:",
    (() => {
      const largeArray = Array.from({ length: 10000 }, (_, i) => i)
      const start = performance.now()
      const result = GroupBy.groupByWithMap(largeArray, x => x % 100, x => x * 2)
      const end = performance.now()
      return (end - start) < 50 && Object.keys(result).length === 100
    })()
  )

  console.log(
    "8. 数组分组性能测试 - 排序分组:",
    (() => {
      const largeArray = Array.from({ length: 5000 }, () => Math.floor(Math.random() * 1000))
      const start = performance.now()
      const result = GroupBy.groupByWithSort(largeArray, x => x % 100, (a, b) => a - b)
      const end = performance.now()
      return (end - start) < 100 && Object.keys(result).length <= 100
    })()
  )

  console.log(
    "9. 数组分组性能测试 - 统计分组:",
    (() => {
      const largeArray = Array.from({ length: 10000 }, (_, i) => ({ id: i % 1000, value: i }))
      const start = performance.now()
      const result = GroupBy.groupByWithStats(largeArray, 'id')
      const end = performance.now()
      return (end - start) < 50 && Object.keys(result.result).length === 1000
    })()
  )

  console.log(
    "10. 数组分组性能测试 - 转换分组:",
    (() => {
      const largeArray = Array.from({ length: 1000 }, (_, i) => ({ id: i % 100, value: i }))
      const start = performance.now()
      const result = GroupBy.groupByWithTransform(largeArray, 'id', {
        [0]: arr => arr.map(x => x.value * 2),
        [1]: arr => arr.map(x => x.value * 3)
      })
      const end = performance.now()
      return (end - start) < 50 && Object.keys(result).length <= 100
    })()
  )

  console.log(
    "11. 数组分组性能测试 - 验证分组:",
    (() => {
      const largeArray = Array.from({ length: 10000 }, (_, i) => i)
      const start = performance.now()
      const result = GroupBy.groupByWithValidation(largeArray, x => x % 100, { min: 0, max: 10000 })
      const end = performance.now()
      return (end - start) < 50 && Object.keys(result).length === 100
    })()
  )

  console.log(
    "12. 数组分组性能测试 - 重复数据:",
    (() => {
      const largeArray = Array.from({ length: 10000 }, () => 1)
      const start = performance.now()
      const result = GroupBy.groupBy(largeArray, x => x)
      const end = performance.now()
      return (end - start) < 50 && Object.keys(result).length === 1
    })()
  )

  console.log(
    "13. 数组分组性能测试 - 无重复数据:",
    (() => {
      const largeArray = Array.from({ length: 1000 }, (_, i) => i)
      const start = performance.now()
      const result = GroupBy.groupBy(largeArray, x => x)
      const end = performance.now()
      return (end - start) < 50 && Object.keys(result).length === 1000
    })()
  )

  console.log(
    "14. 数组分组性能测试 - 并发处理:",
    (() => {
      const largeArray = Array.from({ length: 10000 }, (_, i) => ({ id: i % 1000, value: i }))
      const start = performance.now()
      
      const promises = []
      for (let i = 0; i < 10; i++) {
        promises.push(Promise.resolve().then(() => GroupBy.groupBy(largeArray, 'id')))
      }
      
      return Promise.all(promises).then(results => {
        const end = performance.now()
        return (end - start) < 200 && results.every(r => Object.keys(r).length === 1000)
      })
    })()
  )

  console.log(
    "15. 数组分组性能测试 - 内存泄漏检测:",
    (() => {
      const initialMemory = process.memoryUsage().heapUsed
      
      for (let i = 0; i < 100; i++) {
        const largeArray = Array.from({ length: 1000 }, (_, j) => ({ id: j % 100, value: j }))
        const result = GroupBy.groupBy(largeArray, 'id')
      }
      
      if (global.gc) {
        global.gc()
      }
      
      const finalMemory = process.memoryUsage().heapUsed
      const memoryIncrease = finalMemory - initialMemory
      
      return memoryIncrease < 1024 * 1024 // 内存增长应该小于1MB
    })()
  )
}

testGroupByPerformance()
